import Vue from "vue";
import VueRouter from "vue-router"

// 安装路由
Vue.use(VueRouter);

// 动态路由匹配
const router = new VueRouter({

   mode: 'history',  //改变网址方式

   routes: [

      // 登录
      {
         name: "login",
         path: "/login",
         component: () => import('@/views/login.vue')
      },

      // 首页
      {
         name: "index",
         path: '/index',
         component: () => import("@/views/index.vue"),
         children: [

            // 文章列表
            {
               name: 'articleList',
               path: 'articleList',      //大问题
               component: () => import('@/views/articleList.vue')
            },
            // 文章发布
            {
               name: 'articlePublic',
               path: 'articlePublic',    //大问题
               component: () => import('@/views/articlePublic.vue')
            },

         ]
      },
      {
         path: '/',
         redirect: { name: 'index' }
      }
   ]
});

// 守卫导航
router.beforeEach((to, from, next) => {
   if (to.path == '/login') {
      next();
   } else {
      let token = localStorage.getItem('admin_tokon');
      if (token) {
         next();
      } else {
         next({ name: 'login' });
      }
   }
});

export default router